import React from 'react';

import {message, notice} from '../Notification/index';
import {BaseModal} from '../Modal/index';
import {Modal} from 'antd';
import $$ from 'utils';

class BaseComponent extends React.Component {

  /**
   *
   * @param type success error info warning
   * @param msg 可以是对象{ message: title, description: config,} 或者 字符串
   * @returns {*}
   */
  showNotice = (type, msg) => notice[type](msg);

  /**
   *
   * @param type success error warning loading
   * @param msg  显示的消息 字符串
   * @param delay  延时关闭时间 0 代表永远不关闭
   * @returns {*}  const hide = this.showMsg('loading', 'Action in progress..', 0);  然后调用 hide(), 可以手动关闭对话框
   */
  showMsg = (type, msg, delay) => message.openMessage(type, msg, delay);

  /**
   * 显示对话框
   * @param type  info success error warning confirm(有两个按钮)
   * @param config
   */
  showDialog = (type, config) => {
    return BaseModal.show(type, config);
  }

  /**
   * 界面跳转
   * @param path 路由地址
   */
  jump = (path) => {
    const {history} = this.props;
    history.push(path);
  }

  goBack = () => {
    const {history} = this.props;
    history.goBack();
  }

  /**
   * 异步获取需要的数据
   * @returns {{dict: *, loading: *}}
   */
  getAll = (key) => {

    const {all, loadingAll} = this.getModel(key);

    const dict = (!!all && all.length > 0) ? all.map(v => {
      return ({code: v.id, codeName: v.name})
    }) : undefined;

    return ({loading: loadingAll, dict});
  }

  /**
   * 异步获取需要的数据
   * @returns {{dict: *, loading: *}}
   */
  getTestItem = () => {
    const {all, loadingAll} = this.getModel('testItem');

    const dict = (!!all && all.length > 0) ? all.map(v => {
      return ({code: v.channel, codeName: v.name})
    }) : undefined;

    return ({loading: loadingAll, dict});
  }

  onSearch = (values, isReset) => {
    const {pageNum, pageSize, filters, sorter} = this.state;

    console.log("onSearch", this.state);

    Object.keys(values).forEach(key => {
      values[key] = !!values[key] ? values[key] : null
    });

    console.log("onSearch", values);

    const f = {...filters, ...values};
    this.setState({filters: f});

    this.getPageInfo({pageNum, pageSize: pageSize, filters: f, sorter});
  }

  /**
   * 分页获取数据
   * @param current
   * @param size
   */
  getPageInfo ({pageNum, pageSize, filters, sorter}){

    let f = {};
    console.log("getPageInfo filters", filters)
    if (!!filters) {
      Object.keys(filters).forEach(key => {
        !!filters[key] && (f[key] = filters[key])
      });
    }

    this.dispatch({
      type: `${this.getNamespace()}/getPageInfo`,
      payload: {
        page: {pageNum: (!!pageNum ? pageNum : 1), pageSize: (!!pageSize ? pageSize : 10)},
        filters: f, sorter, notice: this.showMsg
      },
    })
  }

  /**
   * 获取modal数据
   * @param key  设置的namespace
   * @returns {*}
   */
  getModel = (key) => this.props[key];


  dispatch = ({type, payload}) => {
    !!this.props.dispatch && this.props.dispatch({type, payload});
  }

  dispatchNamespace = (namespace) =>{
    this.dispatch({
      type: `${namespace}`,
      payload: {
        notice: this.showMsg
      },
    })
  }

  /**
   * history api 路由跳转
   */
  history = () => {
    const {history} = this.props;
    return history;
  }

  getNamespace() {
    return this.props.namespace;
  }

  handleAdd() {
    /* 子类重写 */
  }

  handleUpdate() {
    /* 子类重写 */
  }

}

export default BaseComponent;
